package tq.com.tqstudent.ui.activity;

import android.content.pm.Signature;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.lib.common.base.BaseActivity;
import com.lib.common.utils.AppUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.ServerBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.net.ok.StringCallback;

/**
 * @Author rames
 * @date 2019/2/27 14:10
 */
public class SplashActivity extends BaseActivity {

    @Override
    public int initLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
//        UserInfo.loginOut();
        getServerUrl();

//        Signature[] ha = AppUtil.getRawSignature(this, "tq.com.tqstudent");
    }

    @Override
    protected boolean setStatusBarTranslate() {
        return false;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            gotoActivity();
        }
    };

    private void gotoActivity() {
        HomeActivity.show(this);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SplashActivity.this.finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(0);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void initData() {

    }

    private void getServerUrl(){
//        String url = "http://www.banqun123.com:5242/svr"; // 测试地址
        String url = "http://www.banqun123.com:7000/svr"; // 正式地址
        OkGo.<String>get(url)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        mHandler.sendEmptyMessageDelayed(0, 1000);
                        ServerBean server = new Gson().fromJson(response.body(), ServerBean.class);
                        UserInfo.setHttpUrl(server.StudentSvr);
                        UserInfo.setIMHttpUrl(server.ImSvr);
                        UserInfo.setFileHttpUrl(server.FileSvr);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        mHandler.sendEmptyMessageDelayed(0, 1000);
                    }
                });
    }
}
